//
//  DLGroupManagerCell.m
//  DontRem
//
//  Created by apple on 2021/1/28.
//

#import "DLGroupManagerCell.h"
#import "DLPswModel.h"
#import "DLGroupListView.h"
@interface DLGroupManagerCell()
@property(nonatomic,retain)UILabel * nameLab;
@property(nonatomic,retain)UIButton * delBtn, *editBtn;
@end
@implementation DLGroupManagerCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor normalColor:UnderLineColor darkColor:UIColorBlack];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createCusUI];
    }
    return self;
}
- (void)setModel:(DLGroupModel *)model{
    _model = model;
    self.nameLab.text = model.groupName;
    if ([model.groupName isEqualToString:@"默认分组"]) {
        self.delBtn.hidden = self.editBtn.hidden = YES;
    }else{
        self.delBtn.hidden = self.editBtn.hidden = NO;
    }
}
-(void)delBtnClick{
    QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(QMUIAlertController *aAlertController, QMUIAlertAction *action) {
    }];
    QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"迁移记录" style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertController *aAlertController, QMUIAlertAction *action) {
        [self choseOtherGroup:self.model.bg_id];
    }];
    QMUIAlertAction *action3 = [QMUIAlertAction actionWithTitle:@"直接删掉" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertController *aAlertController, QMUIAlertAction *action) {
        NSString* pswwhere = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"group_bg_id"),bg_sqlValue(self.model.bg_id)];
        [DLPswModel bg_delete:DLPswRecordTab where:pswwhere];
        
        [self deleteCurrentGroup];
    }];
//    action3.enabled = NO;
    QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"确定删除？" message:@"是否迁移记录到其他分组？" preferredStyle:QMUIAlertControllerStyleActionSheet];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    [alertController showWithAnimated:YES];
}
-(void)deleteCurrentGroup{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"bg_id"),bg_sqlValue(self.model.bg_id)];
    [DLGroupModel bg_delete:DLGroupTab where:where];
    [[NSNotificationCenter defaultCenter] postNotification:[[NSNotification alloc]initWithName:DeleteOrEditGroupNotification object:self userInfo:nil]];
}
-(void)choseOtherGroup:(NSNumber*)unshowId{
    UIViewController * vc = [Mytools currentViewController];
    DLGroupListView * v = [[DLGroupListView alloc]initWithFrame:vc.view.bounds];
    __block __weak typeof(v)weakV = v;
    v.unshowId = unshowId;
    v.dismissEndBlock = ^(DLGroupModel * _Nullable mo) {
        if (mo) {
            NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"group_bg_id"),bg_sqlValue(self.model.bg_id)];
            NSArray * arr = [DLPswModel bg_find:DLPswRecordTab where:where];
            for (DLPswModel * model  in arr) {
                model.group_bg_id = mo.bg_id;
                model.groupName = mo.groupName;
                [model bg_saveOrUpdate];
            }
        }
        [self deleteCurrentGroup];
        [weakV removeFromSuperview];
        XHQHUDSuccessText(@"操作完成");
    };
    [vc.view addSubview:v];
    [v start];
}
-(void)editBtnClick{
    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = @"温馨提示";
    [dialogViewController addTextFieldWithTitle:@"分类名称" configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = @"请输入分类名称";
        textField.maximumTextLength = 10;
    }];
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogTextFieldViewController *aDialogViewController) {
        UITextField * tf = aDialogViewController.textFields.firstObject;
        if (tf.text.length > 0) {
            if ([tf.text isEqualToString:@"默认分组"]) {
                XHQHUDErrorText(@"不能与系统关键词重名，请重新选择");
            }else{
                [aDialogViewController hide];
                self.model.groupName = tf.text;
                [self.model bg_saveOrUpdate];
                [[NSNotificationCenter defaultCenter] postNotification:[[NSNotification alloc]initWithName:DeleteOrEditGroupNotification object:self userInfo:nil]];
            }
        } else {
            XHQHUDErrorText(@"分类名称不能为空");
        }
    }];
    [dialogViewController show];
}
-(void)createCusUI{
    UIView * bgView = [UIView new];
    bgView.cornerRadius = 5;
    bgView.backgroundColor = [UIColor normalColor:UIColorWhite darkColor:UIColorGrayDarken];
    [self.contentView addSubview:bgView];
    bgView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(10, 10, 0, 10));
    
    self.nameLab = [UILabel labelWithTitle:@"" color:[UIColor normalColor:TextColor darkColor:UIColorWhite] fontSize:floor(AUTO(16))];
    [bgView addSubview:self.nameLab];
    self.nameLab.sd_layout.leftSpaceToView(bgView, 10).centerYEqualToView(bgView).heightIs(floor(AUTO(16)));
    [self.nameLab setSingleLineAutoResizeWithMaxWidth:300];
    
    self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.delBtn setImage:Image(@"tag_close") forState:UIControlStateNormal];
    [self.delBtn addTarget:self action:@selector(delBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.delBtn];
    self.delBtn.sd_layout.rightEqualToView(bgView).centerYEqualToView(bgView).heightIs(40).widthEqualToHeight();
    
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.editBtn setImage:Image(@"编辑分组名称") forState:UIControlStateNormal];
    [self.editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.editBtn];
    self.editBtn.sd_layout.rightSpaceToView(self.delBtn, 0).centerYEqualToView(bgView).heightIs(40).widthEqualToHeight();
    
}

@end
